#include "debug.h"

#define DEBUG
	
#ifdef DEBUG
#include <stdarg.h>
#include <stdio.h>
#endif	

 unsigned char UsartData;
 unsigned int UsartCount=0;


  void MomeryCopy(char *source,char *des,unsigned long leng)
  {
  	u32 i;
  	for(i = 0;i<leng;i++)
	{
		*des++ = *source++;
	}
  }

unsigned int GetUsartCount(void)
 {
 	return UsartCount;	
 }

 void SetUsartCount(u16 tem)
 {
 	UsartCount = tem;
 }

 unsigned char GetUsartData(void)
 {
 	return UsartData;	
 }

 void SetUsartData(u8 tem)
 {
 	UsartData = tem;
 }
#ifdef DEBUG
void Debug_printf(char *fmt,...)
{
	char printf_buf[128];
	va_list v_list;
	char *pt;
	va_start(v_list,fmt);
	vsprintf(printf_buf,fmt,v_list);
	va_end(v_list);

	pt = printf_buf;
	printf_buf[127] = 0;

	while(*pt!=0)
	{
		USART_SendData(USART1, *pt);
		pt++;
		while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); 
	}	  
}
#else
	void Debug_printf(char *fmt,...)
	{
	
	}
#endif



void Uart1_init(unsigned int  Baud)
{
	NVIC_InitTypeDef   NVIC_Initstructure;
	GPIO_InitTypeDef   GPIO_InitStructure;
	USART_InitTypeDef  USART_Initstructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_Initstructure.NVIC_IRQChannel                    = USART1_IRQn; 
	NVIC_Initstructure.NVIC_IRQChannelSubPriority         = 1;
	NVIC_Initstructure .NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_Initstructure.NVIC_IRQChannelCmd                 = ENABLE;
	NVIC_Init(&NVIC_Initstructure);

	USART_Initstructure.USART_BaudRate            = Baud;
	USART_Initstructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Initstructure.USART_Mode                = USART_Mode_Rx|USART_Mode_Tx;
	USART_Initstructure.USART_Parity              = USART_Parity_No;
	USART_Initstructure.USART_StopBits            = USART_StopBits_1;
	USART_Initstructure.USART_WordLength          = USART_WordLength_8b;
	USART_Init(USART1,&USART_Initstructure);

	USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	
	USART_Cmd(USART1,ENABLE);		
}
/*
void USART_DMA_Config(void)
{
	DMA_InitTypeDef DMA_InitStucture;

	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_DMA1,ENABLE);
	DMA_DeInit(DMA1_Channel4);
	
	DMA_InitStucture.DMA_MemoryBaseAddr = (unsigned int)Usart_Send_Buffer;
	DMA_InitStucture.DMA_PeripheralBaseAddr = (unsigned int )&USART1->DR;
	DMA_InitStucture.DMA_DIR =DMA_DIR_PeripheralDST;
	DMA_InitStucture.DMA_MemoryDataSize = SendBuffSize;
	DMA_InitStucture.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStucture.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStucture.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStucture.DMA_MemoryDataSize =DMA_MemoryDataSize_Byte;
	DMA_InitStucture.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStucture.DMA_Priority = DMA_Priority_High;
	DMA_InitStucture.DMA_M2M = DMA_M2M_Disable;
	DMA_Init( DMA1_Channel4, &DMA_InitStucture);
	USART_DMACmd(USART1, USART_DMAReq_Tx,ENABLE);	
}
*/
